#  0.0   Initiation                ----
#  1.0   Load packages             ----
library(tidyverse)
library(rio)
library(multcomp)
library(gtsummary)
library(ebal)
library(anesrake)

#  2.0   Load data                 ----
ds <- read.csv("data/survey-data.csv")

#  3.0   Clean data                ----
ds <- ds %>% 
  filter(attention == "Slightly interested") %>% 
  rename(duration = `Duration..in.seconds.`) %>%
  dplyr::select(
    Experimental_Treatment_D_DO, Experimental_Treatment_R_DO,
    party, inds_party, therm_d_1, therm_r_1, effectiveness_d, 
    effectiveness_r,pleased_d, pleased_r, money_d, money_r,
    responsibility_d, responsibility_r, approve_d, approve_r,
    race, income, age, gender, education.1, ideo, zip, duration
  ) %>% 
  mutate(
    full_treatment = case_when(
      Experimental_Treatment_D_DO == 
        "control|therm_d|effectiveness_d|money_d|responsibility_d|pleased_d|approve_d" ~ "control_d",
      Experimental_Treatment_D_DO ==
        "no_com_d|therm_d|effectiveness_d|money_d|responsibility_d|pleased_d|approve_d" ~ "nocom_d",
      Experimental_Treatment_D_DO ==
        "com_mem_d|therm_d|effectiveness_d|money_d|responsibility_d|pleased_d|approve_d" ~ "commem_d",
      Experimental_Treatment_D_DO ==
        "com_lead_d|therm_d|effectiveness_d|money_d|responsibility_d|pleased_d|approve_d" ~ "comlead_d",
      Experimental_Treatment_R_DO ==
        "control_r|therm_r|effectiveness_r|money_r|responsibility_r|pleased_r|approve_r" ~ "control_r",
      Experimental_Treatment_R_DO ==
        "no_com_r|therm_r|effectiveness_r|money_r|responsibility_r|pleased_r|approve_r" ~ "nocom_r",
      Experimental_Treatment_R_DO ==
        "com_mem_r|therm_r|effectiveness_r|money_r|responsibility_r|pleased_r|approve_r" ~ "commem_r",
      Experimental_Treatment_R_DO ==
        "com_lead_r|therm_r|effectiveness_r|money_r|responsibility_r|pleased_r|approve_r" ~ "comlead_r"
    ), 
    pos_treatment = case_when(
      full_treatment=='control_d'|full_treatment=='control_r' ~ 'control',
      full_treatment=='nocom_d'|full_treatment=='nocom_r' ~ 'nocom',
      full_treatment=='commem_d'|full_treatment=='commem_r' ~ 'commem',
      full_treatment=='comlead_d'|full_treatment=='comlead_r' ~ 'comlead'
    ),
    pos_treatment = factor(
      pos_treatment, 
      levels = c('control', 'nocom','commem','comlead')
    ),
    pos_treatment_ref = factor(
      pos_treatment, 
      levels = c('nocom','control','commem','comlead')
    ),
    par_treatment = case_when(
      full_treatment=='control_d'|full_treatment=='nocom_d'|
        full_treatment=='commem_d'|full_treatment=='comlead_d' ~ 'Dem',
      full_treatment=='control_r'|full_treatment=='nocom_r'|
        full_treatment=='commem_r'|full_treatment=='comlead_r' ~ 'Rep'
    ),
    partyr = case_when(
      party=="Not very strong Democrat" | party=="Strong Democrat" ~ "dem",
      party=="Not very strong Republican" | party=="Strong Republican" ~ "rep",
      party=="Independent" & 
        inds_party=="More similar to Democrats than Republicans" ~ "ind",
      party=="Independent" & 
        inds_party=="More similar to Republicans than Democrats" ~ "ind"
    ),
    partyr2 = case_when(
      party=="Not very strong Democrat" | party=="Strong Democrat" ~ "dem",
      party=="Not very strong Republican" | party=="Strong Republican" ~ "rep",
      party=="Independent" & 
        inds_party=="More similar to Democrats than Republicans" ~ "dem",
      party=="Independent" & 
        inds_party=="More similar to Republicans than Democrats" ~ "rep"
    ),
    partymatch = factor(
      case_when(
        partyr2 == 'rep'& par_treatment == 'Rep'~1,
        partyr2 == 'dem'& par_treatment == 'Dem'~1,
        partyr2 == 'rep'& par_treatment == 'Dem'~0,
        partyr2 == 'dem'& par_treatment == 'Rep'~0
      )
    ),
    partymatch2 = case_when(
      partyr == 'rep' & par_treatment == 'Rep' ~ 1,
      partyr == 'dem' & par_treatment == 'Dem' ~ 1,
      partyr == 'rep' & par_treatment == 'Dem' ~ -1,
      partyr == 'dem' & par_treatment == 'Rep' ~ -1,
      partyr == 'ind' & par_treatment == 'Dem' ~ 0,
      partyr == 'ind' & par_treatment == 'Rep' ~ 0
    ),
    therm = case_when(
      therm_d_1!="" ~ as.numeric(therm_d_1),
      therm_r_1!="" ~ as.numeric(therm_r_1)
    ),
    effective = case_when(
      effectiveness_d == "Very Effective" |
        effectiveness_r == "Very Effective" ~ 7,
      effectiveness_d == "Effective" |
        effectiveness_r == "Effective" ~ 6,
      effectiveness_d == "Slightly Effective" | 
        effectiveness_r == "Slightly Effective" ~ 5,
      effectiveness_d == "Neither Ineffective Nor Effective" | 
        effectiveness_r == "Neither Ineffective Nor Effective" ~ 4,
      effectiveness_d == "Slightly Ineffective" | 
        effectiveness_r == "Slightly Ineffective" ~ 3,
      effectiveness_d == "Ineffective" | 
        effectiveness_r == "Ineffective" ~ 2,
      effectiveness_d == "Very Ineffective" | 
        effectiveness_r == "Very Ineffective" ~ 1
      ),
    pleased = case_when(
      pleased_d == "Very Pleased" |
        pleased_r == "Very Pleased" ~ 7,
      pleased_d == "Pleased" |
        pleased_r == "Pleased" ~ 6,
      pleased_d == "Slightly Pleased" | 
        pleased_r == "Slightly Pleased" ~ 5,
      pleased_d == "Neither Pleased Nor Displeased" | 
        pleased_r == "Neither Pleased Nor Displeased" ~ 4,
      pleased_d == "Slightly Displeased" | 
        pleased_r == "Slightly Displeased" ~ 3,
      pleased_d == "Displeased" | 
        pleased_r == "Displeased" ~ 2,
      pleased_d == "Very Displeased" | 
        pleased_r == "Very Displeased" ~ 1
      ), 
    money = case_when(
      money_d == "Very Effective" |
        money_r == "Very Effective" ~ 7,
      money_d == "Effective" |
        money_r == "Effective" ~ 6,
      money_d == "Slightly Effective" | 
        money_r == "Slightly Effective" ~ 5,
      money_d == "Neither Ineffective Nor Effective" | 
        money_r == "Neither Ineffective Nor Effective" ~ 4,
      money_d == "Slightly Ineffective" | 
        money_r == "Slightly Ineffective" ~ 3,
      money_d == "Ineffective" | 
        money_r == "Ineffective" ~ 2,
      money_d == "Very Ineffective" | 
        money_r == "Very Ineffective" ~ 1
    ),
    responsibility = case_when(
      responsibility_d == "Very Responsible" |
        responsibility_r == "Very Responsible" ~ 7,
      responsibility_d == "Responsible" |
        responsibility_r == "Responsible" ~ 6,
      responsibility_d == "Slightly Responsible" | 
        responsibility_r == "Slightly Responsible" ~ 5,
      responsibility_d == "Neither Irresponsible Nor Responsible" | 
        responsibility_r == "Neither Irresponsible Nor Responsible" ~ 4,
      responsibility_d == "Slightly Irresponsible" | 
        responsibility_r == "Slightly Irresponsible" ~ 3,
      responsibility_d == "Irresponsible" | 
        responsibility_r == "Irresponsible" ~ 2,
      responsibility_d == "Very Irresponsible" | 
        responsibility_r == "Very Irresponsible" ~ 1
    ),
    approve = case_when(
      approve_d == "Approve" | approve_r == "Approve" ~ 1,
      approve_d == "Disapprove" | approve_r == "Disapprove" ~ 0
    ),
    black = ifelse(grepl("Black or African American", race), 1, 0), 
    hisplat = ifelse(grepl("Hispanic", race), 1, 0),
    asam = ifelse(grepl("Asian American", race), 1, 0),
    natam = ifelse(grepl("Native American or American Indian", race), 1, 0),
    white = ifelse(grepl("White", race), 1, 0),
    rother = ifelse(grepl("Other", race), 1, 0),
    income_cat = case_when(
      income == "Under $10,000" | 
        income == "$10,000-$19,999" |
        income == "$20,000-$29,999" ~ "Under $29,999", 
      income == "$30,000-$39,999" |
        income == "$40,000-$49,999" | 
        income == "$50,000-$59,999" ~ "$30,000-$59,999",
      income == "$60,000-$74,999" |
        income == "$75,000-$89,999" | 
        income == "$90,000-$119,999" ~ "$60,000-$119,999",
      income == "$120,000-$159,999" | 
        income == "$160,000-$200,000" | 
        income == "Over $200,000" ~ "Over $120,000"
    ),
    income_cat = factor(
      income_cat,
      levels = c(
        'Under $29,999','$30,000-$59,999', '$60,000-$119,999','Over $120,000'
      )
    ),
    income = case_when(
      income == "Under $10,000" ~ 1, 
      income == "$10,000-$19,999" ~ 2,
      income == "$20,000-$29,999" ~ 3,
      income == "$40,000-$49,999" ~ 4,
      income == "$50,000-$59,999" ~ 5,
      income == "$30,000-$39,999" ~ 6,
      income == "$60,000-$74,999" ~ 7,
      income == "$75,000-$89,999" ~ 8,
      income == "$90,000-$119,999" ~ 9,
      income == "$120,000-$159,999" ~ 10,
      income == "$160,000-$200,000" ~ 11,
      income == "Over $200,000" ~ 12
    ),
    age = as.numeric(age),
    gender = case_when(gender==1 ~ "male", gender==2 ~ "female"), 
    education = case_when(
      education.1 == 1 | education.1 == 2 |
        education.1 == 3 | education.1 == 4 |
        education.1 == 5 ~ "Less_than_Bachelors",
      education.1 == 6 | education.1 == 7 |
        education.1 == 8 ~ "Bachelors_or_higher"
    ),
    edu = as.factor(education),
    ideo = case_when(
      ideo == "Very conservative" ~ 1,
      ideo == "Conservative" ~ 2,
      ideo == "Slightly conservative" ~ 3, 
      ideo == "Moderate" ~ 4,
      ideo == "Slightly liberal" ~ 5, 
      ideo == "Liberal" ~ 6, 
      ideo == "Very liberal" ~ 7
    ),
    ideos = abs(ideo-4)
  )

#  4.0   Link zip with State/CD    ----
## 4.1   Import zips and merge     ----
zips <- import("data/zips.csv")
zips <- distinct(zips)
ds <- merge(
  ds,
  zips,
  by = "zip", 
  all.x = TRUE, 
  all.y = FALSE
)

# 5.0 Appendix data sets ----
## 5.1 Attention Checks ----
ds_ac <- read.csv("data/survey-data.csv")

ds_ac <- ds_ac %>% 
  rename(duration = `Duration..in.seconds.`) %>%
  mutate(
    full_treatment = case_when(
      Experimental_Treatment_D_DO == 
        "control|therm_d|effectiveness_d|money_d|responsibility_d|pleased_d|approve_d" ~ "control_d",
      Experimental_Treatment_D_DO ==
        "no_com_d|therm_d|effectiveness_d|money_d|responsibility_d|pleased_d|approve_d" ~ "nocom_d",
      Experimental_Treatment_D_DO ==
        "com_mem_d|therm_d|effectiveness_d|money_d|responsibility_d|pleased_d|approve_d" ~ "commem_d",
      Experimental_Treatment_D_DO ==
        "com_lead_d|therm_d|effectiveness_d|money_d|responsibility_d|pleased_d|approve_d" ~ "comlead_d",
      Experimental_Treatment_R_DO ==
        "control_r|therm_r|effectiveness_r|money_r|responsibility_r|pleased_r|approve_r" ~ "control_r",
      Experimental_Treatment_R_DO ==
        "no_com_r|therm_r|effectiveness_r|money_r|responsibility_r|pleased_r|approve_r" ~ "nocom_r",
      Experimental_Treatment_R_DO ==
        "com_mem_r|therm_r|effectiveness_r|money_r|responsibility_r|pleased_r|approve_r" ~ "commem_r",
      Experimental_Treatment_R_DO ==
        "com_lead_r|therm_r|effectiveness_r|money_r|responsibility_r|pleased_r|approve_r" ~ "comlead_r"
    ), 
    pos_treatment = case_when(
      full_treatment=='control_d'|full_treatment=='control_r' ~ 'control',
      full_treatment=='nocom_d'|full_treatment=='nocom_r' ~ 'nocom',
      full_treatment=='commem_d'|full_treatment=='commem_r' ~ 'commem',
      full_treatment=='comlead_d'|full_treatment=='comlead_r' ~ 'comlead'
    ),
    pos_treatment = factor(
      pos_treatment, 
      levels = c('control', 'nocom','commem','comlead')
    ),
    pos_treatment_ref = factor(
      pos_treatment, 
      levels = c('nocom','control','commem','comlead')
    ),
    par_treatment = case_when(
      full_treatment=='control_d'|full_treatment=='nocom_d'|
        full_treatment=='commem_d'|full_treatment=='comlead_d' ~ 'Dem',
      full_treatment=='control_r'|full_treatment=='nocom_r'|
        full_treatment=='commem_r'|full_treatment=='comlead_r' ~ 'Rep'
    ),
    partyr = case_when(
      party=="Not very strong Democrat" | party=="Strong Democrat" ~ "dem",
      party=="Not very strong Republican" | party=="Strong Republican" ~ "rep",
      party=="Independent" & 
        inds_party=="More similar to Democrats than Republicans" ~ "ind",
      party=="Independent" & 
        inds_party=="More similar to Republicans than Democrats" ~ "ind"
    ),
    partyr2 = case_when(
      party=="Not very strong Democrat" | party=="Strong Democrat" ~ "dem",
      party=="Not very strong Republican" | party=="Strong Republican" ~ "rep",
      party=="Independent" & 
        inds_party=="More similar to Democrats than Republicans" ~ "dem",
      party=="Independent" & 
        inds_party=="More similar to Republicans than Democrats" ~ "rep"
    ),
    partymatch = factor(
      case_when(
        partyr2 == 'rep'& par_treatment == 'Rep'~1,
        partyr2 == 'dem'& par_treatment == 'Dem'~1,
        partyr2 == 'rep'& par_treatment == 'Dem'~0,
        partyr2 == 'dem'& par_treatment == 'Rep'~0
      )
    ),
    partymatch2 = case_when(
      partyr == 'rep' & par_treatment == 'Rep' ~ 1,
      partyr == 'dem' & par_treatment == 'Dem' ~ 1,
      partyr == 'rep' & par_treatment == 'Dem' ~ -1,
      partyr == 'dem' & par_treatment == 'Rep' ~ -1,
      partyr == 'ind' & par_treatment == 'Dem' ~ 0,
      partyr == 'ind' & par_treatment == 'Rep' ~ 0
    ),
    therm = case_when(
      therm_d_1!="" ~ as.numeric(therm_d_1),
      therm_r_1!="" ~ as.numeric(therm_r_1)
    ),
    effective = case_when(
      effectiveness_d == "Very Effective" |
        effectiveness_r == "Very Effective" ~ 7,
      effectiveness_d == "Effective" |
        effectiveness_r == "Effective" ~ 6,
      effectiveness_d == "Slightly Effective" | 
        effectiveness_r == "Slightly Effective" ~ 5,
      effectiveness_d == "Neither Ineffective Nor Effective" | 
        effectiveness_r == "Neither Ineffective Nor Effective" ~ 4,
      effectiveness_d == "Slightly Ineffective" | 
        effectiveness_r == "Slightly Ineffective" ~ 3,
      effectiveness_d == "Ineffective" | 
        effectiveness_r == "Ineffective" ~ 2,
      effectiveness_d == "Very Ineffective" | 
        effectiveness_r == "Very Ineffective" ~ 1
    ),
    pleased = case_when(
      pleased_d == "Very Pleased" |
        pleased_r == "Very Pleased" ~ 7,
      pleased_d == "Pleased" |
        pleased_r == "Pleased" ~ 6,
      pleased_d == "Slightly Pleased" | 
        pleased_r == "Slightly Pleased" ~ 5,
      pleased_d == "Neither Pleased Nor Displeased" | 
        pleased_r == "Neither Pleased Nor Displeased" ~ 4,
      pleased_d == "Slightly Displeased" | 
        pleased_r == "Slightly Displeased" ~ 3,
      pleased_d == "Displeased" | 
        pleased_r == "Displeased" ~ 2,
      pleased_d == "Very Displeased" | 
        pleased_r == "Very Displeased" ~ 1
    ), 
    money = case_when(
      money_d == "Very Effective" |
        money_r == "Very Effective" ~ 7,
      money_d == "Effective" |
        money_r == "Effective" ~ 6,
      money_d == "Slightly Effective" | 
        money_r == "Slightly Effective" ~ 5,
      money_d == "Neither Ineffective Nor Effective" | 
        money_r == "Neither Ineffective Nor Effective" ~ 4,
      money_d == "Slightly Ineffective" | 
        money_r == "Slightly Ineffective" ~ 3,
      money_d == "Ineffective" | 
        money_r == "Ineffective" ~ 2,
      money_d == "Very Ineffective" | 
        money_r == "Very Ineffective" ~ 1
    ),
    responsibility = case_when(
      responsibility_d == "Very Responsible" |
        responsibility_r == "Very Responsible" ~ 7,
      responsibility_d == "Responsible" |
        responsibility_r == "Responsible" ~ 6,
      responsibility_d == "Slightly Responsible" | 
        responsibility_r == "Slightly Responsible" ~ 5,
      responsibility_d == "Neither Irresponsible Nor Responsible" | 
        responsibility_r == "Neither Irresponsible Nor Responsible" ~ 4,
      responsibility_d == "Slightly Irresponsible" | 
        responsibility_r == "Slightly Irresponsible" ~ 3,
      responsibility_d == "Irresponsible" | 
        responsibility_r == "Irresponsible" ~ 2,
      responsibility_d == "Very Irresponsible" | 
        responsibility_r == "Very Irresponsible" ~ 1
    ),
    approve = case_when(
      approve_d == "Approve" | approve_r == "Approve" ~ 1,
      approve_d == "Disapprove" | approve_r == "Disapprove" ~ 0
    ),
    black = ifelse(grepl("Black or African American", race), 1, 0), 
    hisplat = ifelse(grepl("Hispanic", race), 1, 0),
    asam = ifelse(grepl("Asian American", race), 1, 0),
    natam = ifelse(grepl("Native American or American Indian", race), 1, 0),
    white = ifelse(grepl("White", race), 1, 0),
    rother = ifelse(grepl("Other", race), 1, 0),
    income_cat = case_when(
      income == "Under $10,000" | 
        income == "$10,000-$19,999" |
        income == "$20,000-$29,999" ~ "Under $29,999", 
      income == "$30,000-$39,999" |
        income == "$40,000-$49,999" | 
        income == "$50,000-$59,999" ~ "$30,000-$59,999",
      income == "$60,000-$74,999" |
        income == "$75,000-$89,999" | 
        income == "$90,000-$119,999" ~ "$60,000-$119,999",
      income == "$120,000-$159,999" | 
        income == "$160,000-$200,000" | 
        income == "Over $200,000" ~ "Over $120,000"
    ),
    income_cat = factor(
      income_cat,
      levels = c(
        'Under $29,999','$30,000-$59,999', '$60,000-$119,999','Over $120,000'
      )
    ),
    income = case_when(
      income == "Under $10,000" ~ 1, 
      income == "$10,000-$19,999" ~ 2,
      income == "$20,000-$29,999" ~ 3,
      income == "$40,000-$49,999" ~ 4,
      income == "$50,000-$59,999" ~ 5,
      income == "$30,000-$39,999" ~ 6,
      income == "$60,000-$74,999" ~ 7,
      income == "$75,000-$89,999" ~ 8,
      income == "$90,000-$119,999" ~ 9,
      income == "$120,000-$159,999" ~ 10,
      income == "$160,000-$200,000" ~ 11,
      income == "Over $200,000" ~ 12
    ),
    age = as.numeric(age),
    gender = case_when(gender==1 ~ "male", gender==2 ~ "female"), 
    education = case_when(
      education.1 == 1 | education.1 == 2 |
        education.1 == 3 | education.1 == 4 |
        education.1 == 5 ~ "Less_than_Bachelors",
      education.1 == 6 | education.1 == 7 |
        education.1 == 8 ~ "Bachelors_or_higher"
    ),
    edu = as.factor(education),
    ideo = case_when(
      ideo == "Very conservative" ~ 1,
      ideo == "Conservative" ~ 2,
      ideo == "Slightly conservative" ~ 3, 
      ideo == "Moderate" ~ 4,
      ideo == "Slightly liberal" ~ 5, 
      ideo == "Liberal" ~ 6, 
      ideo == "Very liberal" ~ 7
    ),
    ideos = abs(ideo-4), 
    ac  = ifelse(attention =="Slightly interested", 1,0), 
    pp = case_when(
      political_party %in% c(1,2,6) ~ "Dem", 
      political_party %in% c(8, 9, 10) ~ "Rep", 
      political_party %in% c(3, 4, 5) ~ "Ind"
    )
  )

## 5.2 Survey Weighting ----
## 5.2.1 Target Demographics ----
ds_weight <- 
  ds %>%
  dplyr::select(
    effective, money, therm, pleased,
    pos_treatment_ref, par_treatment, partymatch,
    partyr2, white, black, hisplat, asam, natam, rother,
    age, gender, edu, ideo, income
  ) %>% 
  drop_na()

pacs <- read.csv('data/pacs.csv')
pacs <- 
  pacs %>% 
  drop_na(
    SEX, SCHL, AGEP, RACAIAN, HISP, RACASN, RACBLK, RACWHT, RACSOR, PWGTP
  ) %>% 
  dplyr::select(
    SEX, SCHL, AGEP, RACAIAN, HISP, RACASN, RACBLK, RACWHT, RACSOR, PWGTP
  )

pacs <- 
  pacs %>% 
  drop_na(
    SEX, SCHL, AGEP, RACAIAN, HISP, RACASN, RACBLK, RACWHT, RACSOR, PWGTP
  ) %>% 
  mutate(
    WHTHISP = ifelse(RACWHT==1 & HISP==1, 1, 0), 
    AGE = case_when(
      AGEP < 25 ~ 'age1824', 
      AGEP < 35 ~ 'age2534',
      AGEP < 45 ~ 'age3544', 
      AGEP < 55 ~ 'age4554',
      AGEP < 65 ~ 'age5564', 
      AGEP >= 65 ~ 'age6599'
    ), 
    MALE = ifelse(SEX=="male", 1,0), 
    age1824 = ifelse(AGE=='age1824', 1, 0),
    age2534 = ifelse(AGE=='age2534', 1, 0),
    age3544 = ifelse(AGE=='age3544', 1, 0),
    age4554 = ifelse(AGE=='age4554', 1, 0),
    age5564 = ifelse(AGE=='age5564', 1, 0), 
    age6599 = ifelse(AGE=='age6599', 1, 0),
    bach = ifelse(SCHL=="Bachelors_or_higher", 1, 0)
  ) %>%
  dplyr::select(
    SEX, SCHL, MALE, RACAIAN, HISP, RACASN, RACBLK, RACWHT, RACSOR, WHTHISP, 
    PWGTP, age1824, age2534, age3544, age4554, age5564, age6599, bach
  )

poststrat_df <- 
  pacs %>% 
  group_by(
    SEX, SCHL, age1824, age2534, age3544, age4554, age5564, age6599, 
    bach, RACAIAN, HISP, RACASN, RACBLK, RACWHT, RACSOR, 
    .drop = FALSE
  ) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

ds_weight <- 
  ds_weight %>%
  mutate(
    whisplat = ifelse(white==1 & hisplat==1, 1, 0), 
    age6 = case_when(
      age < 25 ~ 'age1824', 
      age < 35 ~ 'age2534',
      age < 45 ~ 'age3544', 
      age < 55 ~ 'age4554',
      age < 65 ~ 'age5564', 
      age >= 65 ~ 'age6599'
    ), 
    male = ifelse(gender=="male", TRUE,FALSE), 
    white = as.logical(white),
    black = as.logical(black),
    asam = as.logical(asam),
    natam = as.logical(natam),
    rother = as.logical(rother),
    hisplat = as.logical(hisplat),
    whisplat = as.logical(whisplat), 
    age1824 = ifelse(age6 == 'age1824', TRUE, FALSE),
    age2534 = ifelse(age6 == 'age2534', TRUE, FALSE),
    age3544 = ifelse(age6 == 'age3544', TRUE, FALSE),
    age4554 = ifelse(age6 == 'age4554', TRUE, FALSE),
    age5564 = ifelse(age6 == 'age5564', TRUE, FALSE),
    age6599 = ifelse(age6 == 'age6599', TRUE, FALSE),
    bach = ifelse(edu == "Bachelors_or_higher", TRUE, FALSE)
  ) %>% 
  drop_na()

### 5.2.2 Entropy Balancing ----

PS_MALE <- 
  pacs %>% 
  group_by(MALE, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_RACWHT <- 
  pacs %>% 
  group_by(RACWHT, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_RACBLK <- 
  pacs %>% 
  group_by(RACBLK, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))
PS_RACASN <- 
  pacs %>% 
  group_by(RACASN, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_RACAIAN <- 
  pacs %>% 
  group_by(RACAIAN, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_RACSOR <- 
  pacs %>% 
  group_by(RACSOR, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_HISP <- 
  pacs %>% 
  group_by(HISP, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_WHTHISP <- 
  pacs %>% 
  group_by(WHTHISP, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_bach  <- 
  pacs %>% 
  group_by(bach, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_age1824  <- 
  pacs %>% 
  group_by(age1824, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_age2534  <- 
  pacs %>% 
  group_by(age2534, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_age3544  <- 
  pacs %>% 
  group_by(age3544, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_age4554  <- 
  pacs %>% 
  group_by(age4554, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

PS_age5564  <- 
  pacs %>% 
  group_by(age5564, .drop = FALSE) %>% 
  summarise(n = sum(as.numeric(PWGTP)))

tot <- sum(pacs$PWGTP)

target <- 
  list(
    male = arrange(PS_MALE, desc(MALE))$n / tot, 
    white = arrange(PS_RACWHT, desc(RACWHT))$n / tot,
    black = arrange(PS_RACBLK, desc(RACBLK))$n / tot,
    asam = arrange(PS_RACASN, desc(RACASN))$n / tot,
    natam = arrange(PS_RACAIAN, desc(RACAIAN))$n / tot,
    rother = arrange(PS_RACSOR, desc(RACSOR))$n / tot,
    hisplat = arrange(PS_HISP, desc(HISP))$n / tot,
    whisplat = arrange(PS_WHTHISP, desc(WHTHISP))$n / tot,
    bach = arrange(PS_bach, desc(bach))$n / tot,
    age1824 = arrange(PS_age1824, desc(age1824))$n / tot,
    age2534 = arrange(PS_age2534, desc(age2534))$n / tot,
    age3544 = arrange(PS_age3544, desc(age3544))$n / tot,
    age4554 = arrange(PS_age4554, desc(age4554))$n / tot,
    age5564 = arrange(PS_age5564, desc(age5564))$n / tot
  )

ds_weight$caseid <- 1:nrow(ds_weight)

ds_eb <- 
  ds_weight %>% 
  dplyr::select(
    male, age1824, age2534, age3544, age4554, age5564, bach,
    white, black, asam, natam, rother, hisplat, whisplat
  )

selection_vector <- c(rep(FALSE, nrow(ds_eb)), TRUE)

target_row <- 
  data.frame(
    male = target$male[1],
    white = target$white[1], 
    black = target$black[1], 
    asam = target$asam[1], 
    natam = target$natam[1], 
    rother = target$rother[1], 
    hisplat = target$hisplat[1], 
    whisplat = target$whisplat[1], 
    bach = target$bach[1], 
    age1824 = target$age1824[1],
    age2534 = target$age2534[1],
    age3544 = target$age3544[1],
    age4554 = target$age4554[1],
    age5564 = target$age5564[1]
  )

ds_eb <- rbind(ds_eb,target_row)

eb.out <- ebalance(
  Treatment = selection_vector, 
  X = ds_eb, 
  constraint.tolerance = .00000001, 
  print.level = 3
)

ds_weight$weightEB <- eb.out$w

### 5.2.3 Raking ----
rake_out <- anesrake(
  target, 
  ds_weight,
  caseid = ds_weight$caseid,
  type = "nolim",
  cap = 7, 
  maxit = 1000, 
  verbose = TRUE
)

ds_weight$weightR <- rake_out$weightvec
